package context

import (
	"net/http"
	"src/config"

	jwt "github.com/dgrijalva/jwt-go"
	"github.com/imroc/req"
	"github.com/labstack/echo"
)

// ErrorRes ErrorResponse
type ErrorRes struct {
	Status  int    `json:"status"`
	ErrMsg  string `json:"error"`
	Success bool   `json:"success"`
}

// DataRes DataResponse
type DataRes struct {
	Status  int         `json:"status"`
	Data    interface{} `json:"data"`
	ErrMsg  string      `json:"error"`
	Success bool        `json:"success"`
}

// RetError response error, wrong response
func RetError(c echo.Context, code, status int, errMsg string) error {
	return c.JSON(code, ErrorRes{
		Status:  status,
		ErrMsg:  errMsg,
		Success: false,
	})
}

// RetData response data, correct response
func RetData(c echo.Context, data interface{}) error {
	return c.JSON(http.StatusOK, DataRes{
		Status:  200,
		Data:    data,
		ErrMsg:  "",
		Success: true,
	})
}

// BindGetJSONData bind the json data of method GET
// body must be a point
func BindGetJSONData(url string, param req.Param, body interface{}) error {
	r, err := req.Get(url, param)
	if err != nil {
		return err
	}
	err = r.ToJSON(body)
	if err != nil {
		return err
	}
	return nil
}

// jwt
func GetJWTUserUnionid(c echo.Context) string {
	return c.Get(config.JWTContextKey).(*jwt.Token).Claims.(jwt.MapClaims)["unionid"].(string)
}

func GetJWTUserOpenid(c echo.Context) string {
	return c.Get(config.JWTContextKey).(*jwt.Token).Claims.(jwt.MapClaims)["openid"].(string)
}

func GetJWTUsername(c echo.Context) string {
	return c.Get(config.JWTContextKey).(*jwt.Token).Claims.(jwt.MapClaims)["username"].(string)
}

func GetJWTClaims(c echo.Context) jwt.MapClaims {
	return c.Get(config.JWTContextKey).(*jwt.Token).Claims.(jwt.MapClaims)
}
